// Default empty project template
#include "BangoBlackberry10App.hpp"

#include <QObject>
#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/system/SystemToast>
#include <bb/cascades/TextField>
#include <QCoreApplication>

#include "BangoAgent.h"
using namespace bb::cascades;
using namespace bb::system;


const QString BangoBlackberry10App::TAG = "BB10ApplicationTestApp";


BangoBlackberry10App::BangoBlackberry10App()
{

}

BangoBlackberry10App::~BangoBlackberry10App()
{


}

void BangoBlackberry10App::onStart()
{
	if(!loadQMLScene()){
		qWarning("Failed to load QML Scene");
	}

	//set your configuration options here e.g setting the post url
	//BangoAgent::setPostURL("<post URL here>");

	//then call start session with your application ID
	BangoAgent::onStartSession("<app ID that has been set up in management tools");
}

bool BangoBlackberry10App::loadQMLScene()
{
	// create scene document from main.qml asset
	// set parent to created document to ensure it exists for the whole application lifetime
	QmlDocument *qml = QmlDocument::create("asset:///main.qml");
	qml->setParent(this);
	if(!qml->hasErrors()){
		qml->setContextProperty("_bangoApp", this);
		AbstractPane *root = qml->createRootObject<AbstractPane>();
		_systemToast = root->findChild<SystemToast *>("msgBox");
		_eventName = root->findChild<TextField *>("eventNameTf");
		_eventDetail = root->findChild<TextField *>("eventDetailTf");
		_eventValue = root->findChild<TextField *>("eventValueTf");
		_param1Name = root->findChild<TextField *>("param1NameTf");
		_param1Value = root->findChild<TextField *>("param1ValueTf");
		_param2Name = root->findChild<TextField *>("param2NameTf");
		_param2Value = root->findChild<TextField *>("param2ValueTf");

		//set the created root object as the scene
		Application::instance()->setScene(root);
		return true;

	}

	return false;
}

/*
 * slots that can be used to send start/end session signals
 */

void BangoBlackberry10App::onAsleep()
{
	BangoAgent::onIdle();
}
void BangoBlackberry10App::onAwake()
{
	BangoAgent::onResume();
}
/*
 * use this method to handle a manual application exit
 *
 */
void BangoBlackberry10App::onManualExit()
{
	BangoAgent::onEndSession();
	Application::instance()->extendTerminationTimeout();
	Application::instance()->exit(0);
}

void BangoBlackberry10App::LoggingLevelTest()
{
	/**
	 * To test - change the method scope for BangoAgent::EventNameAllowed() to public
	 * replace the code if (true BangoAgent::EventNameAllowed(event)) ' to 'if (BangoAgent::EventNameAllowed(event))'
	 */
	QList<QString > eventParams = QList<QString >();
	QList<QString > eventSearch = QList<QString >();
	QList<QString > fakeEvent = QList<QString >();
	eventSearch.append(BangoSessionEvents::EVENT_SEARCH);
	eventParams.append(BangoSessionEvents::EVENT_PAGEVIEW);
	fakeEvent.append("Fake event with space");

	BangoAgent::setLoggingLevelEvents(BangoLoggingLevel::LEVEL_1, eventParams);
	BangoAgent::setLoggingLevelEvents(BangoLoggingLevel::LEVEL_2, eventSearch);

	BangoAgent::setLoggingLevelEvents(BangoLoggingLevel::LEVEL_3, QList<QString >());
	BangoAgent::setLoggingLevelEvents(BangoLoggingLevel::LEVEL_4, QList<QString >());
	BangoAgent::setLoggingLevelEvents(BangoLoggingLevel::LEVEL_5, fakeEvent);

	qDebug() << "No Logging Level set\n";

	QVector<QString> testEvents = QVector<QString>();
	testEvents.append(BangoSessionEvents::EVENT_PAGEVIEW);
	testEvents.append(BangoSessionEvents::EVENT_SEARCH);
	testEvents.append(BangoSessionEvents::EVENT_VIDEOSTART);
	testEvents.append(BangoSessionEvents::SESSION_START);
	testEvents.append(BangoSessionEvents::SESSION_END);
	testEvents.append(BangoSessionEvents::SESSION_IDLE_START);
	testEvents.append(BangoSessionEvents::SESSION_IDLE_END);

	foreach(QString event , testEvents){
		if(/*true*/ BangoAgent::EventNameAllowed(event)){
			qDebug() << event + " allowed\n";
		}
		else {
			qDebug() << event + " NOT allowed \n";
		}

	}
	qDebug() << "\nLogging level 1 set \n";
	BangoAgent::setLoggingLevel(BangoLoggingLevel::LEVEL_1);

	foreach(QString event , testEvents){
		if(/*true*/ BangoAgent::EventNameAllowed(event)){
			qDebug() << event + " allowed\n";
		}
		else {
			qDebug() << event + " NOT allowed \n";
		}
	}
	qDebug() << "\nLogging level 2 set \n";
	BangoAgent::setLoggingLevel(BangoLoggingLevel::LEVEL_2);
	foreach(QString event , testEvents){
		if(/*true*/ BangoAgent::EventNameAllowed(event)){
			qDebug() << event + " allowed\n";
		}
		else {
			qDebug() << event + " NOT allowed \n";
		}
	}
	qDebug() << "\nLogging Level 3,4,5 SET \n";
	BangoAgent::setLoggingLevel(BangoLoggingLevel::LEVEL_5);
	foreach(QString event, testEvents){
		if(/*true*/ BangoAgent::EventNameAllowed(event)){
			qDebug() << event + " allowed/n";
		}
		else{
			qDebug() << " NOT allowed/n";
		}
	}

	qDebug() << "\nLogging Level ALL set\n";
	BangoAgent::setLoggingLevel(BangoLoggingLevel::ALL);
	foreach(QString event, testEvents){
		if(/*true*/ BangoAgent::EventNameAllowed(event)){
			qDebug() << event + " allowed/n";
		}
		else{
			qDebug() << event + " NOT allowed \n";
		}
	}
	qDebug() << "End Logging Level Test";
}

void BangoBlackberry10App::Log()
{
	if(EventName() == ""){
		ShowMessage("Please enter an event name");
	}
	switch(GetOverload()){
	case 1:
		BangoAgent::onEvent(EventName());
		break;
	case 2:
		BangoAgent::onEvent(EventName(), EventValue());
		break;
	case 3:
		BangoAgent::onEvent(EventName(), EventValue(), EventDetail());
		break;
	case 4:
		BangoAgent::onEvent(EventName(), EventValue(), EventParameters());
		break;
	default:
		BangoAgent::onEvent(EventName(), EventValue(), EventDetail(), EventParameters());
		break;
	}
	ShowMessage("Log Made");
}

void BangoBlackberry10App::onDemandVideoStart()
{
	switch(GetOverload()){
		case 1:
			BangoAgent::onDemandVideoStart();
			break;
		case 2:
			BangoAgent::onDemandVideoStart(EventValue());
			break;
		case 3:
			BangoAgent::onDemandVideoStart(EventValue(), EventDetail());
			break;
		case 4:
			BangoAgent::onDemandVideoStart(EventValue(), EventParameters());
			break;
		default:
			BangoAgent::onDemandVideoStart(EventValue(), EventDetail(), EventParameters());
			break;
	}
	ShowMessage("onDemandVideoStart");
}

void BangoBlackberry10App::onDemandVideoStop()
{
	switch(GetOverload()){
		case 1:
			BangoAgent::onDemandVideoStop();
			break;
		case 2:
			BangoAgent::onDemandVideoStop(EventValue());
			break;
		case 3:
			BangoAgent::onDemandVideoStop(EventValue(), EventDetail());
			break;
		case 4:
			BangoAgent::onDemandVideoStop(EventValue(), EventParameters());
			break;
		default:
			BangoAgent::onDemandVideoStop(EventValue(), EventDetail(), EventParameters());
			break;
	}
	ShowMessage("onDemandVideoStop");
}

void BangoBlackberry10App::LiveVideoStart()
{
	switch(GetOverload()){
		case 1:
			BangoAgent::onLiveVideoStart();
			break;
		case 2:
			BangoAgent::onLiveVideoStart(EventValue());
			break;
		case 3:
			BangoAgent::onLiveVideoStart(EventValue(), EventDetail());
			break;
		case 4:
			BangoAgent::onLiveVideoStart(EventValue(), EventParameters());
			break;
		default:
			BangoAgent::onLiveVideoStart(EventValue(), EventDetail(), EventParameters());
			break;
	}
	ShowMessage("onLiveVideoStart");
}

void BangoBlackberry10App::LiveVideoStop()
{
	switch(GetOverload()){
		case 1:
			BangoAgent::onLiveVideoStop();
			break;
		case 2:
			BangoAgent::onLiveVideoStop(EventValue());
			break;
		case 3:
			BangoAgent::onLiveVideoStop(EventValue(), EventDetail());
			break;
		case 4:
			BangoAgent::onLiveVideoStop(EventValue(), EventParameters());
			break;
		default:
			BangoAgent::onLiveVideoStop(EventValue(), EventDetail(), EventParameters());
			break;
	}
	ShowMessage("onLiveVideoStop");
}

void BangoBlackberry10App::Search()
{
	switch(GetOverload()){
		case 1:
			BangoAgent::onSearch();
			break;
		case 2:
			BangoAgent::onSearch(EventValue());
			break;
		case 3:
			BangoAgent::onSearch(EventValue(), EventDetail());
			break;
		case 4:
			BangoAgent::onSearch(EventValue(), EventParameters());
			break;
		default:
			BangoAgent::onSearch(EventValue(), EventDetail(), EventParameters());
			break;
	}
	ShowMessage("onSearch");
}

void BangoBlackberry10App::Idle()
{
	switch(GetOverload()){
	case 1:
		BangoAgent::onIdle();
		break;
	case 2:
		BangoAgent::onIdle(EventValue());
		break;
	case 3:
		BangoAgent::onIdle(EventValue(), EventDetail());
		break;
	case 4:
		BangoAgent::onIdle(EventValue(), EventParameters());
		break;
	default:
		BangoAgent::onIdle(EventValue(), EventDetail(), EventParameters());
		break;
	}
	ShowMessage("onIdle");
}

void BangoBlackberry10App::Resume()
{
	switch(GetOverload()){
	case 1:
		BangoAgent::onResume();
		break;
	case 2:
		BangoAgent::onResume(EventValue());
		break;
	case 3:
		BangoAgent::onResume(EventValue(), EventDetail());
		break;
	case 4:
		BangoAgent::onResume(EventValue(), EventParameters());
		break;
	default:
		BangoAgent::onResume(EventValue(), EventDetail(), EventParameters());
		break;
	}
	ShowMessage("onResume");
}

void BangoBlackberry10App::PageView()
{
	switch(GetOverload()){
	case 1:
		BangoAgent::onPageView();
		break;
	case 2:
		BangoAgent::onPageView(EventValue());
		break;
	case 3:
		BangoAgent::onPageView(EventValue(), EventDetail());
		break;
	case 4:
		BangoAgent::onPageView(EventValue(), EventParameters());
		break;
	default:
		BangoAgent::onPageView(EventValue(), EventDetail(), EventParameters());
		break;
	}
	ShowMessage("onPageView");
}

void BangoBlackberry10App::Error()
{
	BangoAgent::onError("TestError", "TestErrorId", TAG);
	ShowMessage("Error Posted!");
}

void BangoBlackberry10App::ViewBatch()
{
	qDebug() << TAG << "\n---Start Batch---\n";
	ShowMessage(BangoAgent::ViewBatch());
	//qDebug() << TAG << BangoAgent::ViewBatch();
	qDebug() << TAG << "\n--- End Batch---\n";
}

void BangoBlackberry10App::Reset()
{
	_eventName->setText("BB10 Event");
	_eventDetail->setText("BB10 Event Detail");
	_eventValue->setText("BB10 Event Value");
	_param1Name->setText("BGO_SOURCE");
	_param1Value->setText("BB10 SOURCE");
	_param2Name->setText("BGO_TERM");
	_param2Value->setText("BB10 TERM");
}

void BangoBlackberry10App::Clear()
{
	_eventName->setText("");
	_eventDetail->setText("");
	_eventValue->setText("");
	_param1Name->setText("");
	_param1Value->setText("");
	_param2Name->setText("");
	_param2Value->setText("");
}

void BangoBlackberry10App::Exit()
{
	ShowMessage("Simulating Session End");
	BangoAgent::onEndSession();
}

void BangoBlackberry10App::PostNow()
{
	BangoAgent::PostEvents();
	ShowMessage("Posted Events");
}

int BangoBlackberry10App::GetOverload()
{
	 /* 1 - Only Event Name Provided
	 * 2 - Event name and value provided
	 * 3 - Event name, value and detail provided
	 * 4 - Event name, value and event parameters provided
	 * 5 - Event name, value, detail and event parameters provided
	 */
	QString eventValue = EventValue();
	QString eventDetail = EventDetail();
	QMap<QString, QString> eventParameters = EventParameters();

	if(eventValue == QString()){
		eventValue = "";
	}
	if(eventDetail == QString()){
		eventDetail = "";
	}
	int retValue;
	if((eventValue.length() <= 0)){
		retValue = 1;
	}
	if((eventDetail.length() <= 0) && eventParameters.isEmpty()){
		retValue = 2;

	}
	if((eventDetail.length() > 0) && eventParameters.isEmpty()){
		retValue = 3;

	}
	if((eventDetail.length() <= 0) && !eventParameters.isEmpty()){
		retValue = 4;

	}
	if((eventDetail.length() > 0) && !eventParameters.isEmpty()){
		retValue = 5;

	}

	return retValue;
}

void BangoBlackberry10App::ShowMessage(QString messageToShow)
{
	_systemToast->setBody(messageToShow);
	_systemToast->show();

}

QString BangoBlackberry10App::EventValue()
{
	return _eventValue->text();
}

QString BangoBlackberry10App::EventName()
{
	return _eventName->text();
}

QString BangoBlackberry10App::EventDetail()
{
	return _eventDetail->text();
}

QMap<QString, QString> BangoBlackberry10App::EventParameters()
{
	QMap<QString, QString> eventParameters = QMap<QString, QString>();
	if(_param1Name->text().length() > 0 || _param2Name->text().length() > 0){
		if(_param1Name->text().length() > 0){
			QString param1Name = _param1Name->text();
			QString param1Value = _param1Value->text();
			eventParameters.insert(param1Name, param1Value);
		}
		if(_param2Name->text().length() > 0){
			QString param2Name = _param2Name->text();
			QString param2Value = _param2Value->text();
			eventParameters.insert(param2Name, param2Value);
		}
	}
	return eventParameters;
}

























